//
//  YMMoreVC.swift
//  NewVersionDemo
//
//  Created by zeng on 2018/3/14.
//  Copyright © 2018年 youmy. All rights reserved.
//  更多机型

import UIKit
import MJRefresh

fileprivate let kCellId = "kCellId"

class YMMoreVC: UIViewController {
    
    lazy var noDataView = YMNoMoreDataView.nibView()
    
    lazy var headerView = YMMoreMenuView.nibView()
    
    lazy var brandMenuV:YMMenuCollectionView = {
        let brandV = YMMenuCollectionView(frame: CGRect(x: 0, y: headerView.frame.maxY, width: ym_screen_width, height: 20))
        return brandV
    }()
    
    lazy var modelMenuV:YMMenuCollectionView = {
        let brandV = YMMenuCollectionView(frame: CGRect(x: 0, y: headerView.frame.maxY, width: ym_screen_width, height: 20))
        return brandV
    }()
    
    lazy var colorMenuV:YMMenuCollectionView = {
        let brandV = YMMenuCollectionView(frame: CGRect(x: 0, y: headerView.frame.maxY, width: ym_screen_width, height: 20))
        return brandV
    }()
    
    var brandBtn:UIButton?
    
    var modelBtn:UIButton?
    
    var colourBtn:UIButton?
    
    var currentMenuV:YMMenuCollectionView?
    
    lazy var maskView:UIView = {
        let maskView = UIView(frame: self.view.bounds)
        maskView.backgroundColor = UIColor.init(white: 0.3, alpha: 0.7)
        return maskView
    }()
    
    lazy var tableView:UITableView = {
        let tableView = UITableView(frame: CGRect(x: 0, y: headerView.frame.maxY, width: ym_screen_width, height: ym_screen_height - headerView.frame.maxY))
        tableView.dataSource = self
        tableView.delegate = self
        
        tableView.register(UINib.init(nibName: "YMDailyListCell", bundle: nil), forCellReuseIdentifier: kCellId)
        tableView.tableFooterView = UIView()
        tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15)
        
        if #available(iOS 11.0, *) {
            tableView.contentInsetAdjustmentBehavior = .never
        } else {
            // Fallback on earlier versions
        }
        
        return tableView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        
        view.addSubview(headerView)
        
        brandMenuV.menus = ["苹果","OPPO","VIVO","华为","三星","坚果","一加","小米","魅族","美图"]
        
        modelMenuV.menus = ["iPhone X","iPhone 8","iPhone 8 Plus","iPhone 7","iPhone 7 Plus","iPhone 6s","iPhone 6s Plus","iPhone 6","iPhone 6 Plus","iPhone 5SE","iPhone 5S","iPhone 5","iPhone 5C","iPhone 4S","iPhone 4"]
        
        colorMenuV.menus = ["全新","99新","95新","9新"]
        
        refresh()
        
        menuProccess()
    }
    
}


// MARK: - 下拉、上拉刷新
extension YMMoreVC{
    func refresh(){
        tableView.mj_header = YMRefresh.headerRefresh(self, #selector(self.lonaNew))
        tableView.mj_footer = YMRefresh.footerRefresh(self, #selector(self.loadMore))
    }
    
    @objc func lonaNew(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.tableView.mj_header.endRefreshing()
        })
    }
    
    @objc func loadMore(){
        DispatchQueue.main.asyncAfter(deadline: .now() + 2, execute: {
            self.tableView.mj_footer.endRefreshingWithNoMoreData()
        })
    }
}

// MARK: - 下拉菜单
extension YMMoreVC{
    
    func menuProccess(){
        headerView.brandBtnBlock = {[weak self] (btn) in
            
            if self?.colourBtn?.isSelected == true{
                self?.colourBtn?.isSelected = false
            }
            
            if self?.modelBtn?.isSelected == true{
                self?.modelBtn?.isSelected = false
            }
            
            self?.brandMenuV.isHidden = false
            self?.modelMenuV.isHidden = true
            self?.colorMenuV.isHidden = true
            
            if self?.currentMenuV != nil {
                self?.brandMenuV.bringSubview(toFront: (self?.currentMenuV)!)
            }
            
            if btn.isSelected == true {
                self?.brandBtn = btn
                self?.brandBtn?.setTitleColor(ym_red_color, for: .normal)
                self?.maskView.addSubview((self?.brandMenuV)!)
                self?.currentMenuV = self?.brandMenuV
                self?.insert()
            }else{
                self?.removeMaskView()
            }
        }
        
        headerView.modelBtnBlock = {[weak self] (btn) in
            if self?.brandBtn?.isSelected == true{
                self?.brandBtn?.isSelected = false
            }
            
            if self?.colourBtn?.isSelected == true{
                self?.colourBtn?.isSelected = false
            }
            
            self?.brandMenuV.isHidden = true
            self?.modelMenuV.isHidden = false
            self?.colorMenuV.isHidden = true
            
            if self?.currentMenuV != nil {
                self?.modelMenuV.bringSubview(toFront: (self?.currentMenuV)!)
            }
            
            if btn.isSelected == true {
                self?.modelBtn = btn
                self?.modelBtn?.setTitleColor(ym_red_color, for: .normal)
                self?.maskView.addSubview((self?.modelMenuV)!)
                self?.currentMenuV = self?.modelMenuV
                self?.insert()
            }else{
                self?.removeMaskView()
            }
        }
        
        headerView.colourBtnBlock = {[weak self] (btn) in
            if self?.brandBtn?.isSelected == true{
                self?.brandBtn?.isSelected = false
            }
            
            if self?.modelBtn?.isSelected == true{
                self?.modelBtn?.isSelected = false
            }
            
            self?.brandMenuV.isHidden = true
            self?.modelMenuV.isHidden = true
            self?.colorMenuV.isHidden = false
            
            if self?.currentMenuV != nil {
                self?.colorMenuV.bringSubview(toFront: (self?.currentMenuV)!)
            }
            if btn.isSelected == true {
                self?.colourBtn = btn
                self?.colourBtn?.setTitleColor(ym_red_color, for: .normal)
                self?.maskView.addSubview((self?.colorMenuV)!)
                self?.currentMenuV = self?.colorMenuV
                self?.insert()
            }else{
                self?.removeMaskView()
            }
            
        }
        
        brandMenuV.didSelBlcok = {[weak self] (title) in
            self?.brandBtn?.setTitle(title, for: .normal)
            self?.brandBtn?.isSelected = !(self?.brandBtn?.isSelected)!
            self?.removeMaskView()
        }
        
        modelMenuV.didSelBlcok = {[weak self] (title) in
            self?.modelBtn?.setTitle(title, for: .normal)
            self?.modelBtn?.isSelected = !(self?.modelBtn?.isSelected)!
            self?.removeMaskView()
        }
        
        colorMenuV.didSelBlcok = {[weak self] (title) in
            self?.colourBtn?.setTitle(title, for: .normal)
            self?.colourBtn?.isSelected = !(self?.colourBtn?.isSelected)!
            self?.removeMaskView()
        }
    }
    
    
    func insert(){
        view.insertSubview(maskView, belowSubview: headerView)
    }
    
    func removeMaskView(){
        brandMenuV.removeFromSuperview()
        modelMenuV.removeFromSuperview()
        colorMenuV.removeFromSuperview()
        maskView.removeFromSuperview()
    }
}

// MARK: - UITableViewDataSource, UITableViewDelegate
extension YMMoreVC: UITableViewDataSource, UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 123.0
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 10
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellId, for: indexPath) as! YMDailyListCell
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
    }
}
